// Shader based on the one from http://fabiensanglard.net/lightScattering/
uniform sampler2D mainTexture;

float decay = 0.94;
float density=2.0;
float weight = 0.10;

vec2 lightPositionOnScreen=vec2(0.5,1.0);
const int NUM_SAMPLES = 100 ;


float rand(vec2 co, float a){
	return fract(sin(dot(co.xy ,vec2(12.9898,78.233+a))) * 43758.5453);
}


void main()
{	
	vec2 deltaTextCoord = vec2( gl_TexCoord[0].st - lightPositionOnScreen.xy );
	vec2 textCoo = gl_TexCoord[0].st;
	deltaTextCoord *= 1.0 /  float(NUM_SAMPLES) * density;
	float illuminationDecay = 1.0;
	
	
	for(int i=0; i < NUM_SAMPLES ; i++)
	{
			textCoo -= deltaTextCoord;
			vec4 sample = texture2D(mainTexture, textCoo +rand(textCoo,float(i))/60.0);
			sample.a = 1.0-sample.a;
			
			sample *= illuminationDecay * weight;
			
			gl_FragColor += sample;
			
			illuminationDecay *= decay;
	}
	
	
//	gl_FragColor *= exposure;
}
